//
//  TLLogin.m
//  Coding_iOS
//
//  Created by taolei-mac on 2017/11/22.
//  Copyright © 2017年 com.lianchuan. All rights reserved.
//


#import "TLLogin.h"
#import "Coding_NetAPIManager.h"

#define kLoginAccount @"account"
#define kLoginPwd @"passowrd"

#define kLoginStatus @"status"

@implementation TLLogin

- (NSString *)toPath
{
    return @"api/v2/account/login";
}

- (NSDictionary *)toParams
{
    NSMutableDictionary *params = @{@"account": self.email,
                                    @"password" : [self.password sha1Str],
                                    @"remember_me" : self.remember_me? @"true" : @"false"
                                    }.mutableCopy;
    if (self.j_captcha.length > 0) {
        params[@"j_captcha"] = self.j_captcha;
    }
    return params;
}

+ (void)setCurrentAccount:(NSString *)account
{
    if (account.length <= 0) {
        return;
    }
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:account forKey:kLoginAccount];
    [defaults synchronize];
}

+ (NSString *)currentAccount
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults objectForKey:kLoginAccount];
}

+ (void)setCurrentPassword:(NSString *)password
{
    if (password.length <= 0) {
        return;
    }
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:password forKey:kLoginPwd];
    [defaults synchronize];
}

+ (NSString *)currentPassword
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [defaults objectForKey:kLoginPwd];
}

+ (void)setLoginStatus:(BOOL)status
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[NSNumber numberWithBool:status] forKey:kLoginStatus];
    [defaults synchronize];
}

+ (BOOL)isLogin
{
    NSNumber *loginStatus = [[NSUserDefaults standardUserDefaults] objectForKey:kLoginStatus];
    NSString *currentAccount = [self currentAccount];
    NSString *currentPassword = [self currentPassword];
    if(loginStatus.boolValue && currentPassword.length > 0 && currentAccount.length > 0){
        NSString *currentAccount = [TLLogin currentAccount];
        NSString *currentPassword = [TLLogin currentPassword];
        TLLogin *login = [[TLLogin alloc] init];
        login.email = currentAccount;
        login.password = currentPassword;
        [[Coding_NetAPIManager sharedManager] request_Login_WithPath:[login toPath] Params:[login toParams] andBlock:^(id data, NSError *error) {
            
            if(error){
                
                [TLLogin setLoginStatus:NO];
                [SVProgressHUD showNetworkError];
            }else{
                NSInteger code = [[data objectForKey:@"code"] integerValue];
                if(code == 0 ){
                    [TLLogin setLoginStatus:YES];
                    [SVProgressHUD dismiss];
                }else{
                    [TLLogin setLoginStatus:NO];
                    NSDictionary *message = [data objectForKey:@"msg"];
                    if(code == 1){
                        
                        [SVProgressHUD showAlert:[message objectForKey:@"password"]];
                    }else if (code == 1044){
                        
                        [SVProgressHUD showAlert:[message objectForKey:@"account"]];
                    }
                }
            }
        }];
        return YES;
    }else{
        
        return NO;
    }
}

@end
